package net.stratusprogramming.school.Game.Battle;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;

import net.stratusprogramming.school.Game.Framework.Battle.Combatant;
import net.stratusprogramming.school.Game.Framework.GUI.Fonts;
import net.stratusprogramming.school.Game.Framework.GUI.TextButton;
import net.stratusprogramming.school.Game.Framework.Items.Usable;
import net.stratusprogramming.school.Game.Framework.Scene.Actor;
import net.stratusprogramming.school.Game.Framework.System.Config;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.System.Logger;
import net.stratusprogramming.school.Game.Framework.System.SaveMgr;

/**
 * This code makes me want to vomit. I'm sorry I ever wrote this.. yuck. I suppose this is what I get for PROcrastinating
 * 
 * @author Lienau
 * @since 5/17/12
 * @version 0.1
 */
public class Menu extends Actor {

	private Battle battle;
	
	private TextButton bItem;
	private TextButton bWeapon;
	private TextButton bSpell;

	private TextButton potion1;
	private TextButton potion2;
	
	private Type menuType;

	public Menu() {
		battle = InstanceMgr.battle;		
		
		menuType = Type.chooseAction;

		// choose action menu
		bItem = new TextButton(Fonts.Arial20pt, "Use Item");
		bItem.setAlignRight(true);
		bItem.setX(Config.WINDOW_WIDTH - 10);
		bItem.setY(Config.WINDOW_HEIGHT - 115);
		bWeapon = new TextButton(Fonts.Arial20pt, "Use Weapon");
		bWeapon.setAlignRight(true);
		bWeapon.setX(Config.WINDOW_WIDTH - 10);
		bWeapon.setY(Config.WINDOW_HEIGHT - 85);
		bSpell = new TextButton(Fonts.Arial20pt, "Use Spell");
		bSpell.setAlignRight(true);
		bSpell.setX(Config.WINDOW_WIDTH - 10);
		bSpell.setY(Config.WINDOW_HEIGHT - 55);

		// choose item menu
		potion1 = new TextButton(Fonts.Arial20pt, "HP Potion");
		potion1.setAlignRight(true);
		potion1.setX(Config.WINDOW_WIDTH - 10);
		potion1.setY(Config.WINDOW_HEIGHT - 115);
		potion2 = new TextButton(Fonts.Arial20pt, "HP Potion");
		potion2.setAlignRight(true);
		potion2.setX(Config.WINDOW_WIDTH - 10);
		potion2.setY(Config.WINDOW_HEIGHT - 85);
			
	}

	@Override
	public void update(int delta) {
		bItem.update(delta);
		bWeapon.update(delta);
		bSpell.update(delta);
		potion1.update(delta);
		potion2.update(delta);
		
		
		if(!InstanceMgr.input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) return;
		
		Combatant player = battle.getCombatants().get(0);
		int numberOfItems = player.getItems().size();
		
		if (menuType == Type.chooseAction) {
			
			if(bItem.isClicked() && numberOfItems > 0) {
				menuType = Type.chooseItem;
			}
			
			if(bWeapon.isClicked()) {
				player.getWeapons().get(0).attack(battle.getCombatants().get(1), player);
			}
			
			if(bSpell.isClicked()) {
				player.getSpells().get(0).attack(battle.getCombatants().get(1), player);
			}
			
		} else if(menuType == Type.chooseItem && numberOfItems > 0){		
			
			if(numberOfItems == 2 && (potion1.isClicked() || potion2.isClicked())) {
				Usable usable = player.getItems().get(0);
				usable.use(player);
				menuType = Type.chooseAction;
			} else if(numberOfItems == 1 && potion1.isClicked()) {
				Usable usable = player.getItems().get(0);
				usable.use(player);
				menuType = Type.chooseAction;
			}
		}
	}

	@Override
	public void render(Graphics g) {
		if (menuType == Type.chooseAction) {
			bItem.render(g);
			bWeapon.render(g);
			bSpell.render(g);
		} else if(menuType == Type.chooseItem) {
			Combatant player = battle.getCombatants().get(0);
			int number = player.getItems().size();
		
			if(number == 2) {
				potion1.render(g);
				potion2.render(g);
			} else if(number == 1) {
				potion1.render(g);
			}
		}
	}

	@Override
	public void save(SaveMgr saveMgr) {
		// TODO Auto-generated method stub

	}

	@Override
	public void load(SaveMgr saveMgr) {
		// TODO Auto-generated method stub

	}

	public Type getMenuType() {
		return menuType;
	}

	public void setMenuType(Type menuType) {
		this.menuType = menuType;
	}

	public enum Type {
		chooseAction, chooseItem, chooseSpell, none
	};

}
